package net.basshista.jsshtunneling;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.UIManager;

import net.basshista.jsshtunneling.beans.HostBean;
import net.basshista.jsshtunneling.beans.Hosts;
import net.basshista.jsshtunneling.beans.TunnelBean;
import net.basshista.jsshtunneling.beans.TunnelBean.TunnelType;
import net.basshista.jsshtunneling.dao.Dao;
import net.basshista.jsshtunneling.jsch.SSHConnection;
import net.basshista.jsshtunneling.ui.HostPanel;
import net.basshista.jsshtunneling.ui.UIActionListener;

import com.jcraft.jsch.JSchException;

public class Main {

    public static String key = "TESTE";

    /**
     * @param args
     * @throws JSchException
     */
    public static void main(String[] args) throws JSchException {

	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

	    Image img;

	    if (System.getProperty("os.name").startsWith("Mac OS X")) {
		img = Toolkit.getDefaultToolkit()
			.getImage(ClassLoader.getSystemClassLoader().getResource("tunnel.png"));
	    } else {
		img = Toolkit.getDefaultToolkit().getImage(
			ClassLoader.getSystemClassLoader().getResource("tunnel16x16.png"));
	    }

	    PopupMenu menu = new PopupMenu();
	    MenuItem item = new MenuItem("Sair");
	    item.addActionListener(new UIActionListener());

	    MenuItem hosts = new MenuItem("Hosts");
	    hosts.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
		    HostPanel panel = new HostPanel();

		    Dao<Hosts> dao = new Dao<Hosts>(Hosts.class);

		    try {
			Hosts hosts = dao.retrive();

			panel.loadGrid(hosts);

			JFrame frame = new JFrame();
			frame.setMinimumSize(new Dimension(200, 200));
			frame.setSize(panel.getSize());
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.add(panel);

			Toolkit kit = Toolkit.getDefaultToolkit();

			frame.setBounds((int) kit.getScreenSize().getWidth() / 2 - frame.getWidth() / 2, (int) kit
				.getScreenSize().getHeight() / 2 - frame.getHeight() / 2, frame.getWidth(),
				frame.getHeight());

			frame.setVisible(true);
			frame.toFront();

		    } catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    } catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }

		}

	    });

	    menu.add(item);
	    menu.add(hosts);

	    TrayIcon tray = new TrayIcon(img, "JSSHTunneling", menu);

	    if (SystemTray.isSupported()) {
		SystemTray systemTray = SystemTray.getSystemTray();
		systemTray.add(tray);
		tray.displayMessage("Test", "Hello", MessageType.INFO);
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    // ignorar erros de GUI
	}

	HostBean hostBean = new HostBean("server4jun.homelinux.org", "jun", 22, "02666");
	List<TunnelBean> tunnels = new ArrayList<TunnelBean>();
	tunnels.add(new TunnelBean(hostBean, 8080, "localhost", 80, TunnelType.Local));
	tunnels.add(new TunnelBean(hostBean, 9091, "localhost", 9091, TunnelType.Local));

	SSHConnection connection = new SSHConnection(tunnels);

	connection.connect();

    }

}